package com.anjie.powerproject.service.impl;

import com.anjie.powerproject.entity.Electricity;
import com.anjie.powerproject.repository.ElectricityRepository;
import com.anjie.powerproject.service.ElectricityPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ElectricityPriceServiceImpl implements ElectricityPriceService {

    @Autowired
    private ElectricityRepository electricityRepository;

    @Override
    public List<Electricity> getElectricityListByGraphId(int graphId) {
        return electricityRepository.getAllByGraphId(graphId);
    }

    @Override
    public void updateElePrice(String startTime,String endTime,Double price,Integer id){
        electricityRepository.updateElePrice(startTime,endTime,price,id);
    }

    @Override
    public void autoUpdateEleType(int graphId){
        List<Electricity> electricities = electricityRepository.getAllByGraphId(graphId);
        double[] prices = electricities.stream().mapToDouble(item->item.getPrice()).sorted().distinct().toArray();
        if(prices.length>1){
            for (Electricity e:electricities){
                if(e.getPrice().compareTo(prices[0]) == 0){
                    electricityRepository.updateEleType(0,e.getId());
                }else if(e.getPrice().compareTo(prices[prices.length-1]) == 0){
                    electricityRepository.updateEleType(2,e.getId());
                }else{
                    electricityRepository.updateEleType(1,e.getId());
                }
            }
        }
    }

//    //将电价数据起止时间格式由double改为 ##:##
//    public static List<CustomElectricity> changeTimeFormat(List<ElectricityBak> electricities){
//        List<CustomElectricity> customElectricities = new ArrayList<>();
//        if(electricities != null){
//            for(ElectricityBak e:electricities){
//                CustomElectricity c = new CustomElectricity();
//                c.setId(e.getId());
//                c.setStartTime(makeFormat(e.getStartTime()));
//                c.setEndTime(makeFormat(e.getEndTime()));
//                c.setPrice(e.getPrice());
//                c.setGraphId(e.getGraphId());
//                c.setEleType(e.getEleType());
//                customElectricities.add(c);
//            }
//        }
//        return customElectricities;
//    }
//
//    public static String makeFormat(double time){
//        StringBuilder stringBuilder = new StringBuilder();
//        String timeStr = Double.toString(time);
//        int[] res = Arrays.stream(timeStr.split("\\.")).mapToInt(Integer::parseInt).toArray();
//        if (res[1] < 10) {
//            res[1] = res[1] * 10;
//        }
//        if(res[1] == 0){
//            stringBuilder.append(res[0]+":00");
//        }else {
//            stringBuilder.append(res[0]+":"+Math.round(res[1]/10.0 * 6));
//        }
//        return stringBuilder.toString();
//    }

    @Override
    public void deleteElePrice(Integer id){
        electricityRepository.deleteById(id);
    }

    @Override
    public Electricity findElePriceById(Integer id){
        return electricityRepository.findElectricityById(id);
    }

    @Override
    public void saveElePrice(List<Electricity> electricities){
        electricityRepository.saveAll(electricities);
    }

}
